"""Download SYNC"""
from pathlib import Path
from aligo import Aligo


class Download:
    def __init__(self):
        with open('refresh_token', 'r') as refresh_token:
            self.ali_obj = Aligo(refresh_token=refresh_token.read())
        self.sync_id = self._get_sync_id()

    def _succeed_download(self):
        # 检查是否下载成功
        if Path('D:\\SYNC').stat().st_mtime > Path('D:\\SYNC.bak').stat().st_mtime:
            return True
        else:
            return False

    def _get_sync_id(self):
        for each in self.ali_obj.get_file_list():
            if each.name == 'SYNC':
                return each.file_id
        return None

    def run(self):
        # 先备份
        sync = Path('D:\\SYNC')
        sync.rename('SYNC.bak')
        self.ali_obj.download_folder(self.sync_id, 'D:\\SYNC')
        if self._succeed_download():
            print('下载成功！SYNC 已是最新！')
        else:
            print('下载失败！SYNC 似乎并未更新！')
